define(['jq','jqa','aj','sw','te'], ()=> {
    // 购物车部分
    $("#head_car").hover(function(){
        $(this).css("background", "#FBFEE9");
        $(".head_car_text").css("color", "#ff6700");
        $("#car_content").css({"width":"300px"}).animate({
            height:"100px"
        },400).finish();
    },function(){
        $(this).css("background", "#424242");
        $(".head_car_text").css("color", "#b0b0b0");
        $("#car_content").css({"width":"300px"}).animate({
            height:"0px"
        },400);
    })
    // 下拉菜单
    $(".menu_li").hover(function(){
        $("#menu_content_bg").css("border","1px solid #D0D0D0");
        $(this).css("color","#ff6700");
        $("#"+$(this).attr("control")).show();
        $("#menu_content_bg").height(230);
    },function(){
        $("#"+$(this).attr("control")).hide();
       $(this).css("color"," #424242");
        $("#menu_content_bg").height(0);
        $("#menu_content_bg").css("border","0px solid #D0D0D0");
    })
    
    $("#find_input").focus(function(){
        $("#find_wrap").css("border","1px solid #ff6700");
        $("#find_but").css("border-left","1px solid #ff6700");
    })
    $("#find_input").blur(function(){
        $("#find_wrap").css("border","1px solid #F0F0F0");
        $("#find_but").css("border-left","1px solid #F0F0F0");
    })
    
    $("#find_but").hover(function(){
        $(this).css({"background":"#ff6700",color:"#fff"});
    },function(){
        $(this).css({"background":"#fff",color:"#424242"});
    })
    // 选项卡内容
    $("#banner_menu_wrap").children().hover(function(){
        $(this).css("background","#ff6700");
        $(this).children(".banner_menu_content").css("border","1px solid #F0F0F0").show();
    },function(){
        $(this).css("background","none");
        $(this).children(".banner_menu_content").css("border","0px solid #F0F0F0").hide();
    })
    // 退出登录
    console.log( $('.exit'));
    $('.exit').click(function(){
        console.log(1);
                 localStorage.removeItem('username')
               localStorage.removeItem('pass')
               $('#login_a').text("登录") 
               $('.exit').hide()
    })
           

//免登陆1
var logintimes=window.localStorage.getItem("logintimes")
var Nowtimes = Date.parse(new Date());
window.localStorage.setItem("logintimes",logintimes)
//86400000是一天
//604800000是一周
if(Number(logintimes)+604800000<=Nowtimes){
    localStorage.removeItem('username')
    localStorage.removeItem('pass')
    $('#login_a').text("登录") 
    $('.exit').hide()
}


    // 轮播
    var mySwiper = new Swiper('.swiper', {
        autoplay:true,// 时间停留3秒自动切换
        effect: 'fade',// 淡入
        keyboard: true,// 键盘和可以操作
    
        loop: true, // 循环模式选项
    
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
        },
    
        // 如果需要前进后退按钮
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
    
    
    })        
    
    // 渲染数据
    class Index{
        constructor (){
            this.conta = document.querySelector('.container-a')
            this.contb = document.querySelector('.container-b')
            this.contc = document.querySelector('.container-c')
            this.contd = document.querySelector('.container-d')
            this.conte = document.querySelector('.container-e')
            this.contf = document.querySelector('.container-f')
            this.contg = document.querySelector('.container-g')
            this.conth = document.querySelector('.container-h')
            this.goods = document.querySelector('.goods')
            this.main = document.querySelector('main')
            this.cart = document.querySelector('.cart_num')
            this.cart_count = document.querySelector('.cart_count')
            this.bTop = document.querySelector('.back-top')
            this.login = document.querySelector('#login_a')
            this.exit = document.querySelector('.exit')
             
      this.init()
        }
        init(){
         this.requesta()
          this.requestb()
          this.cll()
          console.log( this.cll());
        }
        // 利用ajax向后台发送数据渲染页面
        requesta() {
            pAjax({url: 'http://localhost:2205/libs/goods.json'})
            .then(data=>{
                let res = JSON.parse(data).slice(0 , 9)
                this.rendera(res)
            })
            if(localStorage.getItem('username') ){
                this.login.innerHTML ="您已登录"
                this.exit.style.display='inline-block'
            }
        }
         
        rendera(data) {
              this.conta.innerHTML = template('tpl', {list: data})
              this.contb.innerHTML = template('tp2', {list: data})
              this.contc.innerHTML = template('tp3', {list: data})
              this.addEvent(data);
              this.totalNum()
        }
       requestb() {
            pAjax({url: 'http://localhost:2205/libs/goods.json'})
            .then(data=>{
                let res = JSON.parse(data).slice(1 , 11)
                this.renderb(res)
            })
        } 
        renderb(data) {
            this.contd.innerHTML = template('tp4', {list: data})
            this.conte.innerHTML = template('tp5', {list: data})
            this.contf.innerHTML = template('tp6', {list: data})
            this.contg.innerHTML = template('tp7', {list: data})
            this.conth.innerHTML = template('tp8', {list: data})
            this.totalNum()
      }      
       // 点击加入购物车后数量加入到购物车里面并跳转到购物车结算页面
  addEvent(data) {
    this.main.addEventListener("click",(e) => {
        let target = e.target || e.srcElement;
        if (target.className == "g_btn") {
            console.log(1);
          let id = target.dataset.id - 0;
          let goods = data.find((item)=> {  return item.goods_id == id; });
          let list = JSON.parse(localStorage.getItem('cart')) || []
          if(!list.length){
            list.push(goods)
            goods.cart_number = 1
          }else{
            let res =list.some((item)=>{
                return item.goods_id == id
            })
            if(res){
                let index =list.findIndex(item=>{
                    return  item.goods_id == id
                })
                list[index].cart_number++
            }else{
                list.push(goods)
                goods.cart_number = 1
            }
          }
          localStorage.setItem('cart',JSON.stringify(list))
          this.totalNum()
        }
      },false);
  }
 totalNum(){
    let list =JSON.parse(localStorage.getItem('cart')) || []
    let num =0
    list.forEach(item=>{
        num += Number(item.cart_number)
    })
    this.cart.innerHTML =num
    this.cart_count.innerHTML = num
 }                    
   
 cll()
 {     
    window.onscroll=()=>{
    let topScroll =document.body.scrollTop || document.documentElement.scrollTop;//滚动的距离,距离顶部的距离
    if(topScroll >=1100 ){  
        this.bTop.style.display = 'block';
    } else {
        this.bTop.style.display = 'none';
    }
}
}
    }
    new Index()

    
});
 